Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input.Touch

Class Loose
	Inherits Tile
	Private Shared tileSequence As New List(Of Sequence)()
	'private RoomNew room;
	'public int switchButton = 0;
	Public elapsedTimeOpen As Single = 0
	Public timeFall As Single = 0.5F

	Public ReadOnly Property State() As Enumeration.StateTile
		Get
			Return tileState.Value().state
		End Get
	End Property


	Public Sub New(room As RoomNew, Content As ContentManager, tileType As Enumeration.TileType, state As Enumeration.StateTile, NextTileType__1 As Enumeration.TileType)
		MyBase.room = room
		nextTileType = NextTileType__1

		'this.switchButton = switchButton;
		Dim ax As New System.Xml.Serialization.XmlSerializer(tileSequence.[GetType]())

		Dim txtReader As Stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES & tileType.ToString().ToUpper() & "_sequence.xml")

		'TextReader txtReader = File.OpenText(PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES + tileType.ToString().ToUpper() + "_sequence.xml");
		tileSequence = DirectCast(ax.Deserialize(txtReader), List(Of Sequence))

		For Each s As Sequence In tileSequence
			s.Initialize(Content)
		Next

		'Search in the sequence the right type
		'return s.name == tileType.ToString().ToUpper();
		Dim result As Sequence = tileSequence.Find(Function(s As Sequence) s.name = state.ToString().ToUpper())

		If result IsNot Nothing Then
			'AMF to be adjust....
			result.frames(0).SetTexture(Content.Load(Of Texture2D)(PrinceOfPersiaGame.CONFIG_TILES & result.frames(0).value))

			collision = result.collision
			Texture = result.frames(0).texture
		End If
		Type = tileType


		'change statetile element
		tileState.Value().state = state
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

	Public Sub Normal()
		If tileState.Value().state = Enumeration.StateTile.normal Then
			Return
		End If

		tileState.Add(Enumeration.StateTile.normal)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub


	Public Sub Press()
		If tileState.Value().state = Enumeration.StateTile.loose Then
			Return
		End If

		tileState.Add(Enumeration.StateTile.loose)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
		'Fall();
	End Sub

	Public Sub Fall()
		Fall(False)

	End Sub

	Public Sub Fall(force As Boolean)
		If force <> True Then
			If tileState.Value().state = Enumeration.StateTile.loosefall Then
				Return
			End If
		End If

		tileState.Add(Enumeration.StateTile.loosefall)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())

		SyncLock room.tilesTemporaney
			room.tilesTemporaney.Add(Me)
		End SyncLock
		Me.collision = Enumeration.TileCollision.Passable

		'Vector2 v = new Vector2(Position.X, Position.Y);
		'Tile t = room.GetTile(v);
		room.SubsTile(Coordinates, Enumeration.TileType.space)
		't = room.GetTile(v);

	End Sub



	Public Sub Shake()
		If tileState.Value().state <> Enumeration.StateTile.normal Then
			Return
		End If

		tileState.Add(Enumeration.StateTile.looseshake)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

End Class
